<?php namespace App\Utils;


class ShortLinkTool {
    /**
     * 百度短链接工具。参考 http://dwz.cn/api
     * 如果构建短链接失败则返回原url
     * @param $url
     * @author stephen
     */
    public static function  make($url) {
        $data = array('url' => $url);
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,"http://dwz.cn/create.php");
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_POSTFIELDS,$data);
        $strRes = curl_exec($ch);
        curl_close($ch);
        $arrResponse = json_decode($strRes,true);
        //var_dump($arrResponse);
        if($arrResponse['status']==0) {
            return $arrResponse['tinyurl'];
        } else {
            return $url;
        }
    }
}

?>
